/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services;

import Entitys.Drogueria;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author MAURO
 */
public class DrogueriaServices {
         EntityManagerFactory emf= Persistence.createEntityManagerFactory("DrugStoreProPU");
         EntityManager em=emf.createEntityManager();
         
         public void create (Drogueria d){
          em.getTransaction().begin();
          em.persist(d);
          em.getTransaction().commit();
          em.close();
         }
         
         public void delete (Integer p){
          em.getTransaction().begin();
          Drogueria p1=em.find(Drogueria.class, p);
          if(p1!= null)
          em.remove(p1);
          em.getTransaction().commit();
          em.close();
         }
         
         public List<Drogueria> readAll(){
         Query q=em.createNamedQuery("Drogueria.findAll");
         return q.getResultList();
         }
         
         public Drogueria readById(int idd){
         Query q=em.createNamedQuery("Drogueria.findByIdDrogueria");
         q.setParameter("idDrogueria", idd);
         return (Drogueria) q.getSingleResult();
         }
         
         public Drogueria readByName(String namD){
         Query q=em.createNamedQuery("Drogueria.findByNombreDrogueria");
         q.setParameter("nombreDrogueria",namD);
         return (Drogueria) q.getSingleResult();
         }
    
}
